package com.wwwarehouse.contract.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.wwwarehouse.common.activity.base.BaseApplication;
import com.wwwarehouse.contract.bean.SelfPublishBean;

import java.util.List;
import java.util.Map;

import contract.wwwarehouse.com.contract.R;

/**
 * @创建者 chenchao.
 * @创建时间 2017/9/25 22:02
 * @描述 ${TODO}
 */

public class PublishAdapter extends RecyclerView.Adapter<PublishAdapter.PublishViewHolder> {

    private Context mContext;
    private List<SelfPublishBean.TempPbResourcesBean> mTempPbResources;
    private int mHeight;
    private Map mDeleteItemMap;
    private SelfPublishBean.TempPbResourcesBean mTempPbResourcesBean;
    private PublishAdapter.OnDelListener mOnDelListener;
    public PublishAdapter(Context context, List<SelfPublishBean.TempPbResourcesBean> list, int height) {
        mContext = context;
        mTempPbResources = list;
        mHeight = height;
    }



    @Override
    public PublishAdapter.PublishViewHolder onCreateViewHolder(ViewGroup parent, int i) {
        PublishAdapter.PublishViewHolder holder = new PublishAdapter.PublishViewHolder(LayoutInflater.from(mContext).
                inflate(R.layout.can_see_publish_item, parent,
                        false));

        return holder;
    }

    @Override
    public void onBindViewHolder(final PublishViewHolder publishViewHolder, final int i) {
        mTempPbResourcesBean = mTempPbResources.get(i);
        BaseApplication.getApplicationInstance().displayImg(mTempPbResourcesBean.getRsImg(), publishViewHolder.ivGood);
        publishViewHolder.tvName.setText(mTempPbResourcesBean.getRsName());
        if (mOnDelListener != null) {
            publishViewHolder.tvDel.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mOnDelListener.onDel(publishViewHolder.tvDel, i);
                }
            });
        }

        ViewGroup.LayoutParams params = publishViewHolder.itemView.getLayoutParams();
        params.height = mHeight;
        publishViewHolder.itemView.setLayoutParams(params);
    }



    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getItemCount() {
        if (mTempPbResources != null) {
            return mTempPbResources.size();
        }
        return 0;
    }

    public class PublishViewHolder extends RecyclerView.ViewHolder{
        public ImageView ivGood;
        public TextView  tvName;
        public TextView  tvDel;

        public PublishViewHolder(View itemView) {
            super(itemView);
            ivGood = (ImageView) itemView.findViewById(R.id.iv_good);
            tvName = (TextView) itemView.findViewById(R.id.tv_name);
            tvDel = (TextView) itemView.findViewById(R.id.tv_del);
        }
    }

    public interface OnDelListener{
        void onDel(TextView button, int position);
    }

    public void setOnDelListener(PublishAdapter.OnDelListener mOnDelListener){
        this.mOnDelListener = mOnDelListener;
    }



}
